package ArrayPackage;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * @author Lzm
 * @version 1.0
 */
public class generateMatrix_ {
  @Test
  public void generateMatrixTest(){
    int[][] result = generateMatrix(9);

  }
  public int[][] generateMatrix(int n) {
      int[][] result = new int[n][n];
      int startX = 0;
      int startY = 0;
      int x = 0;
      int y = 0;
      int loop = 0;
      int num = 1;
      while(loop <= n / 2){
        for (x = startX ; x < n - startX - 1; x ++){
          result[x][startY] = num++;
        }
        for (y = startY ; y < n - startY - 1 ; y ++){
          result[x][y] = num++;
        }
        for ( ; x > startX ; x--){
          result[x][y] = num++;
        }
        for ( ; y > startY ; y--){
          result[x][y] = num++;
        }
        loop ++;
        startX ++;
        startY ++;
      }
      if (n % 2 != 0){
        result[x][y] = num;
      }
      return result;
  }
}
